<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/orange.png" alt="" />
        <span>MG小象</span>
      </div>
      <span>退出</span>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="toggle-collapse" @click="togglecollapse">|||</div>
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse ="this.iscollapse"
          :collapse-transition= "false"
          :router= "true"
        >
          <!-- 一级菜单 -->
          <el-submenu v-bind:index="item.id.toString()" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-bind:index="'/' + subitem.path" v-for=" subitem in item.children" :key="subitem.id" @click="saveNavState('/' + subitem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边主体 -->
      <el-main>
         <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-s-check',
        101: 'el-icon-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      // 定义侧边栏不折叠
      iscollapse: false,
      //   被激活的链接地址
      activePath: ''
    }
  },
  // 创建生命周期函数
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 获取所有菜单
    async getMenuList () {
      const { data: ret } = await this.$http.get('menus')
      // console.log(ret)
      if (ret.meta.status !== 200) return this.$message.error(ret.meta.msg)
      this.menulist = ret.data
    },
    togglecollapse () {
      this.iscollapse = !this.iscollapse
    },
    // 保存链接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  line-height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  > div {
    font-size: 20px;
    display: flex;
    align-items: center;
  }
  img {
    width: 58px;
    height: 58px;
    border-radius: 50px;
  }
  > span {
    margin-left: 10px;
    color: rgb(47, 216, 98);
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #d1d7eb;
}
.el-menu {
  border-right: none;
}
.toggle-collapse {
  background-color: #707274;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  cursor: pointer;
  letter-spacing: 0.2em;
}
</style>
